<template>
  <div class="dev">
    <!-- 搜索框 -->
    <div class="search">
      <el-input placeholder="请输入通知标题" clearable prefix-icon="el-icon-search" v-model="noticeTitle">
      </el-input>
      <el-button type="primary" icon="el-icon-search" @click="search">查找</el-button>
      <el-button type="success" icon="el-icon-plus" @click="addDialogShow = true">添加</el-button>
    </div>
    <!-- 表格数据 -->
    <div class="table">
      <el-table :data="tableData" ref="isExpand" v-loading="loading" border height="calc(100%-10rem)"
        highlight-current-row stripe style="width: 100%">
        >
        <template slot="empty">
          <el-empty description="暂无数据"></el-empty>
        </template>
        <el-table-column align="center" prop="noticeTitle" label="通知标题">
        </el-table-column>
        <el-table-column align="center" prop="noticeInfo" label="通知内容">
        </el-table-column>
        <el-table-column align="center" prop="tenantIds" label="关联的租户">
        </el-table-column>
        <el-table-column align="center" prop="createTime" label="创建时间">
        </el-table-column>
        <el-table-column align="center" prop="endTime" label="截止时间">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="edit(scope.row)">更新</el-button>
            <el-button type="text" size="small" @click="delFn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination v-show="tableData.length !== 0" background @size-change="handleSizeChange"
        @current-change="handleCurrentChange" :current-page="pageNum" :page-sizes="[5, 10, 15, 20, 25, 30]"
        :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <!-- 添加公告-->
    <div class="AddDialog">
      <el-dialog @close="closeAdd" title="添加公告" :visible.sync="addDialogShow" :close-on-click-modal="true"
        :destroy-on-close="true" width="40%">
        <el-form label-position="right" :model="addForm" ref="addForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="公告标题" prop="noticeTitle">
            <el-input v-model="addForm.noticeTitle" clearable placeholder="请输入标题"></el-input>
          </el-form-item>
          <el-form-item label="公告内容" prop="noticeInfo">
            <el-input v-model="addForm.noticeInfo" clearable placeholder="请输入内容" type="textarea" :rows="3"></el-input>
          </el-form-item>
          <el-form-item label="截止时间" prop="endTime">
            <el-date-picker v-model="addForm.endTime" type="datetime" placeholder="选择截止时间" align="right"
              :picker-options="pickerOptions" value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="关联租户" prop="tenantIds">
            <el-select multiple collapse-tags clearable v-model="addForm.tenantIds" placeholder="请选择型号">
              <el-option v-for="item in tenantList" :key="item.tenantId" :label="item.tenantName" :value="item.tenantId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogShow = false">取 消</el-button>
          <el-button type="primary" @click="addFn" :disabled="addForm.noticeTitle &&
            addForm.noticeInfo &&
            addForm.endTime &&
            addForm.tenantIds.length !== 0
            ? false
            : true
            ">确 定</el-button>
        </span>
      </el-dialog>
    </div>
    <!-- 修改公告-->
    <div class="EditDialog">
      <el-dialog title="编辑公告" :visible.sync="editDialogShow" :close-on-click-modal="true" :destroy-on-close="true"
        width="40%">
        <el-form label-position="right" :model="editForm" ref="editForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="公告标题" prop="noticeTitle">
            <el-input v-model="editForm.noticeTitle" clearable placeholder="请输入标题"></el-input>
          </el-form-item>
          <el-form-item label="公告内容" prop="noticeInfo">
            <el-input v-model="editForm.noticeInfo" clearable placeholder="请输入内容" type="textarea" :rows="3"></el-input>
          </el-form-item>
          <el-form-item label="截止时间" prop="endTime">
            <el-date-picker v-model="editForm.endTime" type="datetime" placeholder="选择截止时间" align="right"
              :picker-options="pickerOptions" value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="关联租户" prop="tenantIds">
            <el-select multiple collapse-tags clearable v-model="editForm.tenantIds" placeholder="请选择型号">
              <el-option v-for="item in tenantList" :key="item.tenantId" :label="item.tenantName" :value="item.tenantId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogShow = false">取 消</el-button>
          <el-button type="primary" @click="editFn" :disabled="editForm.noticeTitle &&
            editForm.noticeInfo &&
            editForm.endTime &&
            editForm.tenantIds.length !== 0
            ? false
            : true
            ">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
  
<script>
import {
  getNoticeApi,
  addNoticeApi,
  getTenantApi,
  getOneNoticeApi,
  editNoticeApi,
  delNoticeApi,
} from "../../../../api/system/notice";
export default {
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: "明天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() + 3600 * 1000 * 24);
              picker.$emit("pick", date);
            },
          },
          {
            text: "后天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() + 3600 * 1000 * 24 * 2);
              picker.$emit("pick", date);
            },
          },

          {
            text: "一周后",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() + 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            },
          },
        ],
        disabledDate: (time) => {
          const minTime = new Date().getTime() - 3600 * 1000 * 24;
          return time.getTime() < minTime;
        },
      },
      addForm: {
        endTime: "",
        noticeInfo: "",
        noticeTitle: "",
        tenantIds: [],
      },
      editForm: {
        endTime: "",
        noticeInfo: "",
        noticeTitle: "",
        tenantIds: [],
      },
      noticeTitle: "",
      addDialogShow: false,
      tableData: [],
      loading: false,
      total: null,
      pageNum: 1,
      pageSize: 10,
      tenantList: [],
      rules: [],
      editDialogShow: false,
    };
  },
  created() {
    this.pageSize = Number(sessionStorage.getItem("pageSize")) || 10;
    this.pageNum = Number(sessionStorage.getItem("pageNum")) || 1;
    this.getTenant();
    setTimeout(() => {
      this.getNotice(this.pageNum, this.pageSize, this.noticeTitle);
    }, 50);
  },
  destroyed() {
    sessionStorage.removeItem("pageNum");
    sessionStorage.removeItem("pageSize");
  },
  methods: {
    // 获取租户列表
    getTenant() {
      getTenantApi().then((res) => {
        if (res.code == 200) {
          this.tenantList = res.data;
        }
      });
    },
    search() {
      this.pageNum = 1;
      this.getNotice(this.pageNum, this.pageSize, this.noticeTitle);
      this.$refs.isExpand.bodyWrapper.scrollTop = 0;
    },
    // 获取公告列表
    getNotice(pageNum, pageSize, noticeTitle) {
      this.loading = true;
      getNoticeApi(pageNum, pageSize, noticeTitle).then((res) => {
        this.total = res.data.total;
        if (res.data.rows.length == 0) {
          this.pageNum = this.pageNum - 1;
          if (this.pageNum > 0) {
            this.search();
          } else {
            this.tableData = res.data.rows;
            this.loading = false;
          }
        } else {
          this.loading = false;
          res.data.rows.forEach((item) => {
            let arr = [];
            item.tenantIds.forEach((it) => {
              this.tenantList.forEach((ed) => {
                if (it == ed.tenantId) {
                  arr.push(ed.tenantName);
                }
              });
            });
            item.tenantIds = arr.join();
          });
          this.tableData = res.data.rows;
        }
      });
    },
    // 添加公告
    addFn() {
      let loading = this.$loading({
        target: document.querySelector(".AddDialog .el-dialog"),
        lock: true,
      });
      addNoticeApi(this.addForm)
        .then((res) => {
          if (res.code == 200) {
            this.search();
            loading.close();
            this.$message.success("添加成功!");
            this.addDialogShow = false;
          }
        })
        .catch((err) => {
          loading.close();
          this.$message.error("添加失败!");
        });
    },
    // 修改公告
    editFn() {
      let loading = this.$loading({
        target: document.querySelector(".EditDialog .el-dialog"),
        lock: true,
      });
      editNoticeApi(this.editId, this.editForm)
        .then((res) => {
          if (res.code == 200) {
            this.search();
            loading.close();
            this.$message.success("修改成功!");
            this.editDialogShow = false;
          }
        })
        .catch((err) => {
          loading.close();
          this.$message.error("修改失败!");
        });
    },
    // 关闭添加公告
    closeAdd() {
      this.addForm = {
        endTime: "",
        noticeInfo: "",
        noticeTitle: "",
        tenantIds: [],
      };
    },
    // 打开修改
    edit(e) {
      this.editId = e.noticeId;
      getOneNoticeApi(e.noticeId).then((res) => {
        if (res.code == 200) {
          this.editForm = res.data;
          this.editDialogShow = true;
        }
      });
    },
    // 删除公告
    delFn(e) {
      this.$confirm("此操作将永久删除该公告, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delNoticeApi(e.noticeId).then((res) => {
            if (res.code == 200) {
              this.search();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 分页
    handleSizeChange(val) {
      this.pageNum = 1;
      this.getNotice(this.pageNum, val, this.noticeTitle);
      sessionStorage.setItem("pageSize", val);
      this.pageSize = val;
      this.$refs.isExpand.bodyWrapper.scrollTop = 0;
    },
    handleCurrentChange(val) {
      this.getNotice(val, this.pageSize, this.noticeTitle);
      sessionStorage.setItem("pageNum", val);
      this.pageNum = val;
      this.$refs.isExpand.bodyWrapper.scrollTop = 0;
    },
  },
};
</script>
  
<style lang="scss" scoped>
.dev {
  padding-top: 15px;
  padding: 0 15px;
  height: calc(100vh - 100px);

  .search {
    margin-bottom: 15px;

    .el-input {
      width: 200px;
    }

    .el-button {
      margin-left: 15px;
    }
  }

  .table {
    height: 100%;

    .el-table {
      height: 70%;
    }

    .el-pagination {
      text-align: center;
      margin-top: 20px;
    }
  }
}
</style>